package com.xianyun.book.domain.user;

import com.alibaba.cola.domain.Entity;
import lombok.Data;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ytt
 */
@Entity
@Data
public class SystemUser  {
    private Integer id;
    /**用户唯一随机ID*/
    private String uuid;
    /**用户名称*/
    private String userName;
    /**昵称*/
    private String nickName;
    /**头像*/
    private String avatarUrl;
    /**性别*/
    private Gender gender;
    /**身份*/
    private Identity identity;
    /**城市*/
    private String city;
    /**省*/
    private String province;
    /**国籍*/
    private String country ;
    /** 邮箱 */
    private String email;
    /** 手机号 */
    private String mobile;
    /** 用户实际名称 */
    private String userTrueName;
    /** 生日 */
    private Date birthday;
    /** 密码 */
    private String password;
    /**收货地址*/
    private List<ReceiveAddress> receiveAddresses;
    /**该用户关联的三方用户*/
    private List<SocialUser> socialUsers;
    /**用户登录记录 默认查找 7天 可以根据业务拓展查找范围*/
    private List<LoginHistory> loginHistories;
    /**账号状态*/
    private AccountStatus accountStatus;
    /**账号创建时间*/
    private Date createTime;
    /**修改时间*/
    private Date updateTime;

    public String getMainlyLoginLocations() {
        List<String> mainlyLocations = new ArrayList<>(5);
        if (null != loginHistories && loginHistories.size() > 0) {
            Map<String, Integer> map = new HashMap<>(this.loginHistories.size() / 2);
            this.loginHistories.forEach(
                    loginHistory -> {
                        String realAddress = loginHistory.getRealAddress();
                        if (map.containsKey(realAddress)) {
                            Integer count = map.get(realAddress);
                            count++;
                            map.put(realAddress, count);
                        } else {
                            map.put(realAddress, 1);
                        }
                    }
            );
            Map<String, Integer> result = map.entrySet().stream()
                    .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (oldValue, newValue) -> oldValue, LinkedHashMap::new));
            mainlyLocations.addAll(result.keySet());
            return mainlyLocations.subList(0, 1).get(0);
        }
        return "";
    }
}
